package com.zhangfei.sys.service.impl;

import com.zhang.entity.Employee;
import com.zhang.entity.Evaluation;
import com.zhang.entity.Retuntity;
import com.zhangfei.cilent.IUserClient;
import com.zhangfei.sys.dao.EvaluationDao;
import com.zhangfei.sys.service.EvaluationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (Evaluation)表服务实现类
 *
 * @author makejava
 * @since 2021-06-25 14:50:03
 */
@Service("evaluationService")
public class EvaluationServiceImpl implements EvaluationService {
    @Resource
    private EvaluationDao evaluationDao;

    @Resource
    private IUserClient iUserClient;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Evaluation queryById(Integer id) {
        return this.evaluationDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Evaluation> queryAllByLimit(int offset, int limit) {
        return this.evaluationDao.queryAllByLimit(offset, limit);
    }

    @Override
    public Retuntity queryAll(String uid, String tid) {
        Employee em = iUserClient.getEm(uid);
        if (em.getIsDard()<4){
            Evaluation evaluation = new Evaluation();
            evaluation.setTid(Integer.valueOf(tid));
            List<Evaluation> evaluations = evaluationDao.queryAll(evaluation);
            return new Retuntity(evaluations,200,"查询成功");
        }
        return Retuntity.error(20003,"权限不够");
    }

    /**
     * 新增数据
     *
     * @param evaluation 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public Evaluation insert(Evaluation evaluation) {
        evaluation.setCreateTime(new Date());
        this.evaluationDao.insert(evaluation);
        return evaluation;
    }

    /**
     * 修改数据
     *
     * @param evaluation 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public Evaluation update(Evaluation evaluation) {
        this.evaluationDao.update(evaluation);
        return this.queryById(evaluation.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.evaluationDao.deleteById(id) > 0;
    }
}
